<!-- data是组件自带的，表示当前节点数据 -->
<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%;"
  >
    <el-col :span="20">{{ nodeData.name }}</el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col :span="12">
          <span>{{ nodeData.manager }}</span>
        </el-col>
        <el-col :span="12">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link" style="font-size:14px" @click.stop>
              操作
              <i class="el-icon-arrow-down"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="edit">
                编辑部门
              </el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="del">
                删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentApi } from '@/api/departments'
export default {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    async handleCommand(type) {
      // console.log(type)
      if (type === 'add') {
        // console.log('添加子部门')
        this.$emit('addDepts', this.nodeData)
      }
      if (type === 'edit') {
        // console.log('编辑部门')
        this.$emit('editDepts', this.nodeData)
      }
      if (type === 'del') {
        // console.log('删除部门')
        const res = await this.$confirm(
          '此操作将永久删除该部门, 是否继续?',
          '提示',
          {
            type: 'warning'
          }
        ).catch(() => {
          this.$message.info('已经取消删除')
        })
        if (res === 'confirm') {
          const {
            data: { message }
          } = await delDepartmentApi(this.nodeData.id)
          // console.log(message)
          this.$message.success(message)
          this.$emit('reload')
        }
        // console.log(res)
      }
    }
  }
}
</script>

<style></style>
